<?php

namespace App\Exports;

use App\Models\FinishProduct;
use Maatwebsite\Excel\Concerns\FromArray;

class PandianExport implements FromArray
{
    /**订单导出**/
    private $params;
    /**
     * @var string[] 导出标题集合
     */
    private $headingList = [
        'id'=>'ID',
        'order_number'=>'订单编号',
        'customer_name'=>'客户名称',
        'serial_number'=>'订单流水号',
        'product_name'=>'产品名称',
        'model_number'=>'型号',
        'specification'=>'规格',
        'color'=>'颜色',
        'direction'=>'方向',
        'remark'=>'备注',
        'order_date'=>'接单日期',
        'delivery_date'=>'交货日期',
        'delivery_time'=>'发货日期',
        'logistics_status' => '发货状态',
        'warehouse_id' => '仓库',
        'goods_shelf_id' => '货架',
        'num' => '件数',
        'is_check'=>'是否盘点',
        'check_time'=>'盘点时间'
    ];



    public function __construct($params) {
        ob_end_clean();
        $this->params = $params;
    }




    /**
     * Notes: 处理导出数据表头
     * User: q
     * DateTime: 2021/10/13 18:10
     * @return array
     */
    public function headings(): array{

        return  [
            "ID",
            "订单编号",
            "客户名称",
            "订单流水号",
            "产品名称",
            "型号",
            "规格",
            "颜色",
            "方向",
            "备注",
            "接单日期",
            "交货日期",
            "发货日期",
            '发货状态',
            '仓库',
            '货架',
            '件数',
            '是否盘点',
            '盘点时间'

        ];
    }


    public function array() : array{
        $data[] = $this->headingList;
        $params = $this->params;
        $array = FinishProduct::Search($params)->where('is_stock_up',2)
            ->with(['order'=>function($q){
                $q->with('check_log');
            },'stock_allocation_log','warehouse','goods_shelf','order_logistics','image1'=>function($q){
                return $q->select('id','file_name','file_url');
            },'image2'=>function($q){
                return $q->select('id','file_name','file_url');
            },'image3'=>function($q){
                return $q->select('id','file_name','file_url');
            },'image4'=>function($q){
                return $q->select('id','file_name','file_url');
            }])->where('logistics_status','<>',3)
            ->orderByRaw("FIELD(logistics_status, 1, 4, 2, 5,3)")
            ->get()->toArray();

        //p($array);
        foreach ($array as $key=>&$val){
            //物流状态1：待发货2未发货3已发货4部分待发货5部分未发货
            if($val['logistics_status'] == 1){
                $logistics_status = '待发货';
            }elseif($val['logistics_status'] == 2){
                $logistics_status = '未发货';
            }elseif($val['logistics_status'] == 3){
                $logistics_status = '已发货';
            }elseif($val['logistics_status'] == 4){
                $logistics_status = '部分待发货';
            }elseif($val['logistics_status'] == 5){
                $logistics_status = '部分未发货';
            }
            if($val['is_check'] == 1){
                $is_check = '已盘点';
            }else{
                $is_check = '未盘点';
            }
            $data[] = [
                'id' => $val['id'],
                'order_number' => $val['order']['order_number']??"",
                'customer_name' => $val['order']['customer']['customer_name']??'',
                'serial_number' => $val['order']['serial_number']??'',
                'product_name' => $val['order']['product']['product_name']??'',
                'model_number' => $val['order']['model_number']['model_number']??'',
                'specification' => $val['order']['specification']['specification']??'',
                'color' => $val['order']['color']['color']??'',
                'direction' => $val['direction']??'',
                'remark' => $val['order']['remark']??'',
                'order_date' => $val['order']['order_date']??'',
                'delivery_date' => $val['order']['delivery_date']??'',
                'delivery_time'=>$val['delivery_time']??'',
                'logistics_status' => $logistics_status,
                'warehouse' => $val['warehouse']['warehouse_name']??'',
                'goods_shelf' => $val['goods_shelf']['goods_shelf_name']??'',
                'num' => $val['num']??'',
                'is_check' => $is_check,
                'check_time' => $val['check_time'],
            ];
        }
        //p($data);
        return $data;
    }
}
